
import subprocess
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout,
                             QPushButton, QTextEdit, QFileDialog, QMessageBox)

class StreamViewerUI(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("视频流信息查看器")
        self.setGeometry(100, 100, 600, 400)

        # 初始化UI元素
        self.video_path = None
        self.init_ui()

    def init_ui(self):
        # 主窗口布局
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        layout = QVBoxLayout()

        # 视频选择按钮
        self.select_btn = QPushButton("选择视频文件")
        self.select_btn.clicked.connect(self.select_video)
        layout.addWidget(self.select_btn)

        # 显示流信息的文本框
        self.result_text = QTextEdit()
        self.result_text.setReadOnly(True)
        layout.addWidget(self.result_text)

        # 执行按钮
        self.run_btn = QPushButton("获取流信息")
        self.run_btn.clicked.connect(self.get_stream_info)
        layout.addWidget(self.run_btn)

        central_widget.setLayout(layout)

    def select_video(self):
        file_path, _ = QFileDialog.getOpenFileName(self, "选择视频文件", "", "视频文件 (*.mp4 *.avi)")
        if file_path:
            self.video_path = file_path
            QMessageBox.information(self, "提示", f"已选择视频: {file_path}")

    def get_stream_info(self):
        """调用ffmpeg命令获取视频流信息"""
        if not self.video_path:
            QMessageBox.warning(self, "警告", "请先选择视频文件")
            return

        try:
            result = subprocess.run(
                ["ffmpeg", "-i", self.video_path, "-hide_banner"],
                stderr=subprocess.PIPE,
                text=True
            )
            self.result_text.setText(result.stderr)
        except FileNotFoundError:
            QMessageBox.critical(self, "错误", "未找到ffmpeg，请确保已安装ffmpeg并添加到系统路径中。")

if __name__ == '__main__':
    app = QApplication([])
    window = StreamViewerUI()
    window.show()
    app.exec_()